Sometimes you want to specify something other than numbers for a random selection, or the numbers you want are not in any real order or within any set range. In these cases you would use choose() to generate a random result. For example, say you want to create an object with a random sprite at the start, then you could use this function to set the sprite index to one of a set of given sprites. Note that you can have as many as you require (note that more arguments will mean that the function will be slower to parse).
NOTE: This function will return the same value every time the game is run afresh due to the fact that GameMaker Studio 2 generates the same initial random seed every time to make debugging code a far easier task. To avoid this behaviour use randomise at the start of your game.
choose(val0, val1, val2... max_val);
Argument | Description |
---|---|
val0... max_val | An input value that can be string, integer, variable or constant. |
One of the given arguments
sprite_index = choose(spr_Cactus, spr_Flower,
spr_Tree, spr_Shrub);
hp = choose(5, 8, 15, 32, 40);
name = choose("John", "Steven", "Graham", "Jack", "Emily", "Tina",
"Jill", "Helen");
The above code uses choose to set a number of properties for the instance.